package com.haogu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haogu.entity.*;

import com.haogu.mapper.ContactsMapper;
import com.haogu.mapper.CustomerMapper;
import com.haogu.service.ContactsService;
import com.haogu.service.LeadsService;
import com.haogu.service.impl.CustomerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    private CustomerService customerService;
    @Resource
    private CustomerMapper customerMapper;
    @Resource
    private ContactsService contactsService;
    @Resource
    private ContactsMapper contactsMapper;
    @Resource
    private LeadsService leadsService;

    /**
     * 查询修改所需信息
     *
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("/cu")
    public String cu(Integer id, ModelMap modelMap) {
        if (id != null) {
            Customer customer = customerService.selectCustomer(id);//根据id查询客户信息
            List<User> userList = customerService.selectByUser();//查询所有用户信息
            modelMap.addAttribute("customer", customer);
            modelMap.addAttribute("userList", userList);
        }
        List<User> userList = customerService.selectListUser();

        modelMap.addAttribute("userList", userList);
        return "customer/change";
    }

    /**
     * 修改
     *
     * @param customer
     * @return
     */
    @RequestMapping("/change")
    @ResponseBody
    public ModelMap change(Customer customer) {
        customerService.change(customer);
        ModelMap m = new ModelMap();
        m.addAttribute("msg", "修改成功");
        return m;
    }

    /**
     * 查询添加前的负责人信息
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/cus")
    public String cus(ModelMap modelMap, Integer leadsId) {
        List<User> userList = customerService.selectListUser();
        modelMap.addAttribute("userList", userList);
        //线索有关的代码开始
        modelMap.addAttribute("leadsId", leadsId);
        //线索有关代码结束
        return "customer/add";
    }

    /**
     * 添加客户信息
     *
     * @param customer
     * @param contacts
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap save(Customer customer, Contacts contacts, Integer leadsId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        customer.setCreatoruserid(user.getId());//设置创建者id


        customerService.insertcustomer(customer);//插入客户

        QueryWrapper<Customer> ow = new QueryWrapper<>();
        ow.eq("name", customer.getNames());

        Customer c1 = customerMapper.selectOne(ow);//根据name获取客户id
        contacts.setCustomer_id(c1.getId());//客户id设置进联系人的customer_id

        contacts.setCreator_user_id(user.getId());
        customerService.insertContacts(contacts);//插入联系人
        ModelMap m = new ModelMap();
        m.addAttribute("msg", "添加成功");

        //线索有关代码开始
        QueryWrapper<Contacts> ow2 = new QueryWrapper<>();
        ow2.eq("name", contacts.getName());
        Contacts contacts1 = contactsMapper.selectOne(ow2);
        User user1 = (User) session.getAttribute("user");
        leadsService.updateTransformedInfoById(leadsId, user1.getId(), contacts1.getId(), c1.getId());
        //线索有关代码结束
        return m;
    }

    /**
     * 展示所有客户
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/lis")
    @ResponseBody
    public Map list(Integer page, Integer rows, HttpSession session) {
        //我的客户查询
        Object customerLists1 = session.getAttribute("customerLists1");
        if (customerLists1 != null) {
            Map map = new HashMap<>();
            map.put("result", customerLists1);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("customerLists1");
            return map1;
        }

        //日、周、月新建信息查询
        Object customerLists = session.getAttribute("customerLists");
        if (customerLists != null) {
            Map map = new HashMap<>();
            map.put("result", customerLists);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("customerLists");
            return map1;
        }

        //关注查询
        Object batchConcerned = session.getAttribute("batchConcerned");
        if (batchConcerned != null) {
            Map map = new HashMap<>();
            map.put("result", batchConcerned);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("batchConcerned");
            return map1;
        }

        //客户根据条件模糊查询
        Object customerList = session.getAttribute("customerList");
        if (customerList != null) {
            Map map = new HashMap<>();
            map.put("result", customerList);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("customerList");
            return map1;
        }

        //查询所有
        Map map = new HashMap();
        int rows1 = customerService.rows();
        List<CustomerList> list = customerService.selectCustomerList(page, rows);

        map.put("totalRows", rows1);
        map.put("result", list);
        Map map1 = new HashMap();

        map1.put("data", map);
        return map1;
    }

    /**
     * 跳客户展示主页
     *
     * @return
     */
    @RequestMapping("/list")
    public String li() {
        return "/customer/list";
    }

    /**
     * 跳客户管理模块
     *
     * @return
     */
    @RequestMapping("/CustomerAction_list")
    public String customerAction_list() {
        return "customer/customer";
    }

    /**
     * 展示某一条信息
     *
     * @return
     */
    @RequestMapping("/sho")
    public String show(Integer id, Model model) {
        Customer customer = customerService.selectCustomer(id);
        List<Contacts> contacts = contactsService.selectContactsList(customer.getId());

        model.addAttribute("customer", customer);
        model.addAttribute("list", contacts);
        return "/customer/show";
    }

    /**
     * 客户根据条件模糊查询
     *
     * @param filterSearch
     * @param search
     * @return
     */
    @RequestMapping("/search")
    public String search(String filterSearch, String search, HttpSession session) {
        List<CustomerList> customerList = customerService.selectByCustomer(search, filterSearch);
        if (customerList != null) {
            session.setAttribute("customerList", customerList);
        }
        return "customer/list";
    }

    /**
     * 客户池根据条件模糊查询
     *
     * @param filterSearch
     * @param search
     * @return
     */
    @RequestMapping("/searchs")
    public String searchs(String filterSearch, String search, HttpSession session) {
        List<CustomerList> customerListss = customerService.selectByCustomers(search, filterSearch);
        if (customerListss != null) {
            session.setAttribute("customerListss", customerListss);
        }
        return "customer/list_customers";
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/toDel")
    @ResponseBody
    public ModelMap toDel(String id, HttpSession session) {
        int id1 = Integer.parseInt(id);
        int i = customerService.delCustomers(id1);

        User user = (User) session.getAttribute("user");
        Integer id2 = user.getId();//获取登录者id
        customerService.deleteTime(id2, Integer.valueOf(id));//填充删除时间及删除者的id

        ModelMap m = new ModelMap();
        if (i == 1) {
            m.addAttribute("msg", "删除成功");
            return m;
        }
        m.addAttribute("msg", "删除失败");
        return m;
    }

    /**
     * 跳转发送邮件界面
     *
     * @return
     */
    @RequestMapping(value = "/emails")
    public String email(Integer id, HttpSession session, ModelMap modelMap) {
        //获取邮件接收者
        Customer customer = customerService.selectCustomer(id);
        String zipcode = customer.getZipcode();
        modelMap.addAttribute("zipcode", zipcode);
        //获取邮件发送者
        User user = (User) session.getAttribute("user");
        String email = user.getEmail();
        modelMap.addAttribute("email", email);
        return "marketing/sendemail_marketing";
    }

    /**
     * 发送邮件
     *
     * @param zipcode 接收者
     * @param email   发送者
     * @param subject 邮件标题
     * @param content 邮件内容
     * @return
     */
    @RequestMapping("toSendMail")
    @ResponseBody
    public Map sendMail(String zipcode, String email, String subject, String content) {
        customerService.sendSimpleMail(email, zipcode, subject, content);
        Map map = new HashMap();
        map.put("data", true);
        return map;
    }

    /**
     * 发送短信（待定）
     *
     * @return
     */
    @RequestMapping(value = "/chat")
    public String chat(Integer id,ModelMap modelMap) {
        DuanXin duanXin = customerService.selectChat(id);
        modelMap.addAttribute("dx", duanXin);
        return "marketing/sendmsg_marketing";
    }

    /**
     * 今日、今周、今月查询
     *
     * @param type
     * @param session
     * @return
     */
    @RequestMapping("/toSelectNewCreateDate")
    public String selectNewCreateDate(@RequestParam("type") String type, HttpSession session) {
        List<CustomerList> customerLists = customerService.todayNewCreateSelect(type);
        if (customerLists != null) {
            session.setAttribute("customerLists", customerLists);
        }
        return "customer/list";
    }

    /**
     * 我的客户查询
     *
     * @param session
     * @return
     */
    @RequestMapping("/toMyCustomer")
    public String toMyCustomer(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        List<CustomerList> customerLists = customerService.myCustomer(id);
        session.setAttribute("customerLists1", customerLists);
        return "customer/list";
    }

    /**
     * 跳客户池页面
     *
     * @return
     */
    @RequestMapping("/toCustomers")
    public String toCustomers(HttpSession session) {
        List<CustomerList> customers = customerService.selectCustomers();
        session.setAttribute("customers", customers);
        return "/customer/list_customers";
    }

    /**
     * 展示客户池中的所有客户
     *
     * @param session
     * @return
     */
    @RequestMapping("/toLis")
    @ResponseBody
    public Map toLis(Integer page, Integer rows, HttpSession session) {
        //客户池查询
        Object customers = session.getAttribute("customers");
        if (customers != null) {
            Map map = new HashMap<>();
            int i = customerService.rows1();//获取客户池总数
            map.put("totalRows", i);
            map.put("result", customers);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("customers");
            return map1;
        }
        //客户池根据条件模糊查询
        Object customerListss = session.getAttribute("customerListss");
        if (customerListss != null) {
            Map map = new HashMap<>();
            map.put("result", customerListss);
            Map map1 = new HashMap<>();
            map1.put("data", map);
            session.removeAttribute("customerListss");
            return map1;
        }
        return null;
    }

    /**
     * 将客户放入到客户池
     *
     * @return
     */
    @RequestMapping("/toReleaseCustomers")
    public String toReleaseCustomers(@RequestParam("id") Integer id) {
        customerService.toReleaseCustomers(id);
        return "customer/customer";
    }

    /**
     * 批量关注
     * @param id
     * @return
     */
    @RequestMapping("/toBatchConcerned")
    public String toBatchConcerned(@RequestParam("id") Integer id) {
        customerService.toBatchConcerned(id);
        return "customer/customer";
    }

    /**
     * 关注查询
     * @param session
     * @return
     */
    @RequestMapping("/selectBatchConcerned")
    public String selectBatchConcerned(HttpSession session) {
        List<CustomerList> batchConcerned = customerService.selectBatchConcerned();
        if (batchConcerned != null) {
            session.setAttribute("batchConcerned", batchConcerned);
        }
        return "customer/list";
    }

    /**
     * 改变关注图标
     * @param customerId
     * @return
     */
    @RequestMapping("/batchCon")
    @ResponseBody
    public Map batchCon(@Param("customerId") Integer customerId) {
        Map map = new HashMap();
        Customer cuss = customerService.cuss(customerId);
        if (cuss.getIsLocked() != null) {
            if (cuss.getIsLocked() == 1) {
                map.put("success", true);
            }
        } else {
            map.put("success", false);
        }
        return map;
    }

    /**
     * 取消关注
     * @param id
     * @return
     */
    @RequestMapping("/toCancelConcerned")
    public String toCancelConcerned(@RequestParam("id") Integer id) {
        customerService.toCancelConcerned(id);
        return "customer/customer";
    }
}
